package com.yeqifu.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yeqifu.bus.entity.Goods;
import com.yeqifu.bus.entity.Type;
import com.yeqifu.bus.service.IGoodsService;
import com.yeqifu.bus.service.ITypeService;
import com.yeqifu.bus.vo.TypeVo;
import com.yeqifu.sys.common.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @since 2020-12-06
 */
@RestController
@RequestMapping("/type")
public class TypeController {

    @Autowired
    private ITypeService typeService;

    @Autowired
    private IGoodsService goodsService;

    /**
     * 查询商品日期信息
     * @param
     * @return
     */
    @RequestMapping("loadAllType")
    public DataGridView loadAllType(TypeVo typevo){
        IPage<Type> page = new Page<Type>(typevo.getPage(),typevo.getLimit());
        QueryWrapper<Type> queryWrapper = new QueryWrapper<Type>();
        //名称模糊查询
        queryWrapper.like(StringUtils.isNotBlank(typevo.getType()),"type",typevo.getType());
        //对时间进行查询要求大于开始时间小于结束时间
        queryWrapper.orderByAsc("id");
        typeService.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    /**
     * 加载所有可用的类型名称，用于修改和添加商品信息时需要类型下拉信息展示
     * @return   list
     */
    @RequestMapping("loadAllTypeNameSelect")
    public DataGridView loadAllTypeNameSelect(){
        QueryWrapper<Type> queryWrapper = new QueryWrapper<Type>();
        List<Type> type = typeService.searchalltype();
        queryWrapper.isNotNull("type");
        List<Type> list = typeService.list(queryWrapper);
        return new DataGridView(list);
    }
    /*
     * 添加类型
     * @param goodsVo
     * @return
     */
    @RequestMapping("addtype")
    public ResultObj addtype(TypeVo typevo){
        try {
            //获取该名称类型
            System.out.println(typevo.getType());
            List<Type> type=typeService.searchexist(typevo.getType());
            if(type.size()==0)
            {
                typeService.save(typevo);
                return ResultObj.ADD_SUCCESS;
            }
            else
            {
                return ResultObj.ADD_TYPE_EXIST;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /*
     * 删除类型
     * @param id 商品id
     * @return
     */
   @RequestMapping("deletetype")
    public ResultObj deletetype(String type,int id){
        try {
            System.out.println(type);
            System.out.println(id);
           List<Goods> goods = goodsService.searchgoodsbytype(type);
           if(goods.size()==0)//表明该类型下没有商品
           {
               typeService.deletetypebyid(id);
               return ResultObj.DELETE_SUCCESS;
           }
           else
           {
               return ResultObj.DELETE_TYPE_ERROR;
           }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /*
     * 修改商品
     * @param goodsVo
     * @return
     */
/*
    @RequestMapping("updatetype")
    public ResultObj updatetype(TypeVo typevo){
        try {
            //获取该名称类型
            System.out.println(typevo.getType());
            List<Type> type=typeService.searchexist(typevo.getType());
            if(type.size()==0)
            {
                typeService.updateById(typevo);
                return ResultObj.UPDATE_SUCCESS;
            }
            else
            {
                return ResultObj.ADD_TYPE_EXIST;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }
*/


}
